package at.fhooe.mc.master.clc.server.dataaccess.dao.objectify;

import java.util.List;

import at.fhooe.mc.master.clc.server.dataaccess.dao.ITestableDao;
import at.fhooe.mc.master.clc.server.domain.ITestable;

import static at.fhooe.mc.master.clc.server.dataaccess.dao.objectify.OfyService.ofy;

public class TestableObjectifyDao implements ITestableDao
{

	@Override
	public int save(ITestable testable)
	{
		ofy().save().entity(testable).now();
		return 0;
	}

	@Override
	public int save(List<ITestable> testables)
	{
		ofy().save().entities(testables).now();
		return 0;
	}

	@Override
	public List<? extends ITestable> loadAll(Class<? extends ITestable> type)
	{
		return ofy().load().type(type).list();
	}

	@Override
	public List<? extends ITestable> loadFilteredEquals(Class<? extends ITestable> type, String field1,
			Object filter1, String field2, Object filter2, String field3,
			Object filter3, String field4, Object filter4)
	{
		if (field1 != null && field2 != null && field3 != null && field4 != null)
		{
			return ofy().load().type(type).filter(
					field1 + " =", filter1).filter(
							field2 + " =", filter2).filter(
									field3 + " =", filter3).filter(
											field4 + " =", filter4).list();
		}
		
		if (field1 != null && field2 != null && field3 != null)
		{
			return ofy().load().type(type).filter(
					field1 + " =", filter1).filter(
							field2 + " =", filter2).filter(
									field3 + " =", filter3).list();
		}
		
		if (field1 != null && field2 != null)
		{
			return ofy().load().type(type).filter(
					field1 + " =", filter1).filter(
							field2 + " =", filter2).list();
		}
		
		if (field1 != null)
		{
			return ofy().load().type(type).filter(
					field1 + " =", filter1).list();
		}
		
		return ofy().load().type(type).list();
	}
	

	@Override
	public ITestable loadSpecific(Class<? extends ITestable> type, long id)
	{
		return ofy().load().type(type).id(id).now();
	}

	@Override
	public ITestable loadSpecific(Class<? extends ITestable> type, String id)
	{
		return ofy().load().type(type).id(id).now();
	}

	@Override
	public int delete(ITestable testable, Class<? extends ITestable> specificClass)
	{
		ofy().delete().entity(testable).now();
		return 0;
	}

	@Override
	public int deleteAll(List<ITestable> toBeDeleted)
	{
		ofy().delete().entities(toBeDeleted).now();
		return 0;
	}

}
